package com.example.dao.impl;

import com.example.dao.ContactUserDao;
import com.example.entity.customerAdmin.ContactUser;
import com.example.utils.BaseDao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ContactUserDaoImpl implements ContactUserDao {
    @Override
    public int add (ContactUser contactUser) {

        int i=-1;
        try {
            Object[] objects=new Object[13];
            objects[0]=contactUser.getName ();
            objects[1]=contactUser.getPhone ();
            objects[2]=contactUser.getRole ();
            objects[3]=contactUser.getDeptJob ();
            objects[4]=contactUser.getBirthday ();
            objects[5]=contactUser.getFixedPhone();
            objects[6]=contactUser.getTransPhone();
            objects[7]=contactUser.getEmail();
            objects[8]=contactUser.getQq();
            objects[9]=contactUser.getArea();
            objects[10]=contactUser.getAddress();
            objects[11]=contactUser.getTip();
            objects[12]=contactUser.getSex();
            String sql="insert into contact_user(name,phone,role,dept_job,birthday,fixedPhone,transPhone,email,qq,area,address,tip,sex) value(?,?,?,?,?,?,?,?,?,?,?,?,?)";
            i= BaseDao.modify (sql,objects);
        }catch (Exception e){
            e.printStackTrace ();
        }
        return i;
    }

    @Override
    public int delete (int id) {
        int i=-1;
        try {
            Object[] objects=new Object[1];
            objects[0]=id;
            i= BaseDao.modify ("delete from contact_user where id=?",objects);
        }catch (Exception e){
            e.printStackTrace ();
        }
        return i;
    }

    @Override
    public int update (ContactUser contactUser) {
        int i=-1;
        try {
            Object[] objects=new Object[6];
            objects[0]=contactUser.getName ();
            objects[1]=contactUser.getPhone ();
            objects[2]=contactUser.getRole ();
            objects[3]=contactUser.getDeptJob ();
            objects[4]=contactUser.getBirthday ();
            objects[5]=contactUser.getId ();
            String sql="update contact_user set name=?,phone=?,role=?,dept_job=?,birthday=? where id=?";
            i= BaseDao.modify (sql,objects);
        }catch (Exception e){
            e.printStackTrace ();
        }
        return i;
    }

    @Override
    public List<ContactUser> selectAll (int page, int limit) {
        List<ContactUser> contactUserList=new ArrayList<> ();

        Object[] objects=new Object[2];
        objects[0]=page;
        objects[1]=limit;
        ResultSet select = BaseDao.select ("select * from contact_user limit ?,?", objects);
        try {
            while (select.next ()){
                ContactUser contactUser=new ContactUser ();
                contactUser.setId (select.getInt ("id"));
                contactUser.setName (select.getString ("name"));
                contactUser.setPhone (select.getString ("phone"));
                contactUser.setRole (select.getString ("role"));
                contactUser.setDeptJob (select.getString ("dept_job"));
                contactUser.setBirthday (select.getString ("birthday"));
                contactUser.setEmail(select.getString("email"));
                contactUser.setFixedPhone(select.getString("fixedPhone"));
                contactUser.setTransPhone(select.getString("transPhone"));
                contactUser.setQq(select.getString("qq"));
                contactUser.setArea(select.getString("area"));
                contactUser.setAddress(select.getString("tip"));
                contactUserList.add (contactUser);
            }
        }catch (Exception e){
            e.printStackTrace ();
        }
        return contactUserList;
    }

    @Override
    public ContactUser selectId (int id) {
        ContactUser contactUser=new ContactUser ();
        Object[] objects=new Object[1];
        objects[0]=id;
        ResultSet select = BaseDao.select ("select * from contact_user where id=?", objects);
        try {
            while (select.next ()){
                contactUser.setId (select.getInt ("id"));
                contactUser.setName (select.getString ("name"));
                contactUser.setPhone (select.getString ("phone"));
                contactUser.setRole (select.getString ("role"));
                contactUser.setDeptJob (select.getString ("dept_job"));
                contactUser.setBirthday (select.getString ("birthday"));
                contactUser.setEmail(select.getString("email"));
                contactUser.setFixedPhone(select.getString("fixedPhone"));
                contactUser.setTransPhone(select.getString("transPhone"));
                contactUser.setQq(select.getString("qq"));
                contactUser.setArea(select.getString("area"));
                contactUser.setAddress(select.getString("tip"));
            }
        }catch (Exception e){
            e.printStackTrace ();
        }
        return contactUser;
    }

    @Override
    public int getCount () {

        int i=0;
        ResultSet select = BaseDao.select ("select count(*) sumContact from contact_user ",null);
        try {
            while (select.next ()){
                i=select.getInt ("sumContact");
            }
        }catch (Exception e){
            e.printStackTrace ();
        }
        return i;
    }

    @Override
    public ContactUser selectByName(String newContact) {
        Object[]objects=new Object[1];
        objects[0]=newContact;
        ResultSet resultSet = BaseDao.select("select *from contact_user where name=?", objects);
        try {
            while (resultSet.next()){
                ContactUser contactUser=new ContactUser();
                contactUser.setId (resultSet.getInt ("id"));
                contactUser.setName (resultSet.getString ("name"));
                contactUser.setPhone (resultSet.getString ("phone"));
                contactUser.setRole (resultSet.getString ("role"));
                contactUser.setDeptJob (resultSet.getString ("dept_job"));
                contactUser.setBirthday (resultSet.getString ("birthday"));
                contactUser.setEmail(resultSet.getString("email"));
                contactUser.setFixedPhone(resultSet.getString("fixedPhone"));
                contactUser.setTransPhone(resultSet.getString("transPhone"));
                contactUser.setQq(resultSet.getString("qq"));
                contactUser.setArea(resultSet.getString("area"));
                contactUser.setAddress(resultSet.getString("tip"));
                return contactUser;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return null;
    }

    @Override
    public List<ContactUser> selectHelpContact(int id) {
        List<ContactUser>contactUserList=new ArrayList<>();
        List<Integer>helpList=new ArrayList<>();
        Object[]objects=new Object[1];
        objects[0]=id;
        ResultSet resultSet = BaseDao.select("select *from help_contact where contactid=?", objects);
        try {
            while (resultSet.next()){
                Integer helpId=resultSet.getInt("helpid");
                helpList.add(helpId);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        for (Integer helpId:helpList) {
            ContactUser contactUser = selectId(helpId);
            contactUserList.add(contactUser);
        }
        return contactUserList;
    }
}
